<?php
/**
 * 教室信息类
 */
namespace app\admin\controller;

use think\App;
use think\facade\View;
use app\admin\model\Studentmodel;
use app\admin\model\Classifymodel;
use app\admin\model\Classhourmodel;
use app\admin\model\Organizationmodel;
use app\admin\validate\Student as validate;

class Student extends Common
{
    protected $cfmodel;
    protected $csmodel;
    protected $omodel;

    public function __construct(App $app)
    {
        $this->validate = validate::class;
        $this->validateType = 'student';
        $this->localModel = new Studentmodel();
        parent::__construct($app);
        $this->cfmodel = new Classifymodel();
        $this->csmodel = new Classhourmodel();
        $this->omodel = new Organizationmodel();
    }

    public function index()
    {
        $where = $showData = $showDataArr = $croomWhereData = [];
        $showData['catData'] = $this->cfmodel->findAllInfo('id, name', [['fid', '=', 21]]);
        $showData['lanData'] = $this->cfmodel->findAllInfo('id, name', [['fid', '=', 35]]);
        $showData['sexData'] = $this->cfmodel->findAllInfo('id, name', [['fid', '=', 32]]);

        $dataArr = $this->request->request('search/a', []);

        if($dataArr['nickname'])  $where[] = ['nickname', 'like', "%".$dataArr['nickname']."%"];
        if($dataArr['username'])  $where[] = ['username', 'like', "%".$dataArr['username']."%"];
        if($dataArr['mobile'])    $where[] = ['mobile', 'like', "%".$dataArr['mobile']."%"];
        if($dataArr['status'])    $where[] = ['status', '=', $dataArr['status']];
        if($dataArr['catid'])     $where[] = ['catid', '=', $dataArr['catid']];
        if($dataArr['lanid'])     $where[] = ['lanid', '=', $dataArr['lanid']];
        if($dataArr['sexid'])     $where[] = ['sexid', '=', $dataArr['sexid']];

        $list = $this->localModel->findAllPageInfo('*', $where, ['id'=>'desc'], $this->request->param());
        $showDataArr = ['data'=>$list['data'], 'pageData'=>$list['page'], 'dataArr'=>$dataArr, 'page'=>$this->request->get('page')];
        View::assign(array_merge($dataArr, $showDataArr, $showData));
        return view();
    }

    public function add(){
        $id = $this->request->get('id', 0);

        if($id){
            $showData['id'] = $id;
            $showData['detail'] = $this->localModel->findDetaiInfoById('*', $id);
            View::assign($showData);
            return view('edit');
        }else{
            return view();
        }
    }

    public function getStudent(){
        $showData = [];
        $showData['oid'] = $this->request->get('oid', 0);
        $showData['csid'] = $this->request->get('csid', 0);

        $where = [['is_delete', '=', 1], ['status', '=', 1]];
        if($showData['oid'])  $where[] = ['oid', '=', $showData['oid']];
        $showData['odata'] = $this->omodel->findAllInfo('id, title', [['status', '=', 1]]);
        $showData['data'] = $this->localModel->findAllInfo($select=  "id, username", $where);
        $showData['ids'] = $this->csmodel->getStudensIds($showData['csid']);
        View::assign($showData);
        return view('checkindex');
    }

}
